package com.hj.controller;

import com.alibaba.fastjson.JSONObject;
import com.hj.model.AdvanceResponse;
import com.hj.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品类别 的Controller
 *
 * @author haoshen
 * @version 1.0
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/cate")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询全部根分类 即level=1
     *
     * @return
     */
    @GetMapping("/listCate")
    public AdvanceResponse selAll() {
        return categoryService.selAll();
    }

    /**
     * 由id查询名称
     */
    @GetMapping("/cateName")
    public AdvanceResponse getName(Integer id) {
        return categoryService.getName(id);
    }

    /**
     * 增加一个分类
     */
    @PutMapping("/cate")
    public AdvanceResponse addCate(@RequestBody String msg) {
        JSONObject object = JSONObject.parseObject(msg);
        return categoryService.addCate(object.getInteger("pid"), object.getString("name"));
    }

    /**
     * 查找类的子类
     */
    @GetMapping("/moreCate")
    public AdvanceResponse more(Integer id) {
        return categoryService.more(id);
    }

    /**
     * 删除一个分类
     */
    @GetMapping("/delCate")
    public AdvanceResponse delCate(Integer id) {
        return categoryService.delCate(id);
    }

    /**
     * 修改分类名称
     */
    @GetMapping("/changeName")
    public AdvanceResponse changeName(String name, Integer id) {
        return categoryService.changeName(name, id);
    }
}
